package utils

import (
	"path/filepath"

	"github.com/docker/docker/client"
)

func GetDockerClient(serverUrl, certPath string) (*client.Client, error) {
	destUrl := serverUrl
	if destUrl == "" {
		destUrl = "unix:///var/run/docker.sock"
	}
	opts := []client.Opt{
		client.WithHost(serverUrl),
	}
	if certPath != "" {
		caFile := filepath.Join(certPath, "ca.pem")
		certFile := filepath.Join(certPath, "cert.pem")
		keyFile := filepath.Join(certPath, "key.pem")
		opts = append(opts, client.WithTLSClientConfig(caFile, certFile, keyFile))
	}
	return client.NewClientWithOpts(opts...)
}
